package com.ruicar.afs.cloud.apply.pre.loan.mq.sender.controller;

import com.alibaba.csp.sentinel.util.StringUtil;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.apply.business.service.ApplySequenceService;
import com.ruicar.afs.cloud.apply.common.entity.PreApproveInfo;
import com.ruicar.afs.cloud.apply.common.utils.ApplyConstants;
import com.ruicar.afs.cloud.apply.contract.service.CreatContractService;
import com.ruicar.afs.cloud.apply.pre.approve.condition.PreApproveCondition;
import com.ruicar.afs.cloud.apply.pre.approve.service.PreApproveService;
import com.ruicar.afs.cloud.apply.pre.loan.mq.sender.CaseSubmitInfoSender;
import com.ruicar.afs.cloud.apply.pre.loan.mq.sender.impl.PreApproveSenderImpl;
import com.ruicar.afs.cloud.bizcommon.print.enums.ServiceClientTypeEnum;
import com.ruicar.afs.cloud.common.core.enums.AfsEnumUtil;
import com.ruicar.afs.cloud.common.core.security.util.SecurityUtils;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.common.modules.dto.mq.approveprev.ApprovePrevInfoDto;
import com.ruicar.afs.cloud.common.mq.rabbit.message.AfsTransEntity;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 预审批MQ推送
 *
 * @author 耿继辉
 */
@RestController
@AllArgsConstructor
@RequestMapping("/pre")
@ConditionalOnProperty(prefix = "com.ruicar.afs.cloud.mq.rabbit", name = "enable")
@Slf4j
public class PreApproveSenderController {
    /**
     * 预审批service
     */
    private final PreApproveService preApproveService;
    private final PreApproveSenderImpl preApproveSenderImpl;
    private final CaseSubmitInfoSender caseSubmitInfoSender;

    /**
     * 提交预审批信息-发送消息到消费者
     *
     * @return
     */
    @PostMapping("/preApproveMessageSender")
    @ApiOperation("提交预审批信息")
    public IResponse preApproveMessageSender(@RequestBody PreApproveCondition condition) {
        log.info("{}", condition);
        JSONArray jsonArray = SecurityUtils.getUser().getUserExtInfo().getJSONArray(ApplyConstants.DEPT_IDS);
        if (jsonArray != null) {
            condition.setDeptIds(jsonArray.toJavaList(String.class));
            if (condition.getDeptIds().size() == 1) {
                condition.setDeptId(condition.getDeptIds().get(0));
            }
        }
        //查询预审批信息
        PreApproveInfo approveInfo = preApproveService.getOne(Wrappers.<PreApproveInfo>query().lambda()
                .eq(StringUtil.isNotEmpty(condition.getBusinessType()), PreApproveInfo::getBusinessType, condition.getBusinessType())
                .eq(StringUtil.isNotEmpty(condition.getCertNo()), PreApproveInfo::getCertNo, condition.getCertNo())
                .eq(StringUtil.isNotEmpty(condition.getCustName()), PreApproveInfo::getCustName, condition.getCustName())
                .eq(StringUtil.isNotEmpty(condition.getDeptId()), PreApproveInfo::getDeptId, condition.getDeptId()));
        //更新预审批状态为审核中
        approveInfo.setApplyStatus(ApplyConstants.PRE_PENDDING);
        AfsTransEntity<ApprovePrevInfoDto> transEntity = this.preApproveSenderImpl.returnPreApproveInfo(approveInfo);
        this.caseSubmitInfoSender.preApproveMessageSender(transEntity);
        //更新预审批信息
        preApproveService.updateById(approveInfo);
        return IResponse.success("提交成功");
    }
}
